<?php


namespace app\common\helper;

use Elasticsearch\ClientBuilder as ESClientBuilder;
class Elasticsearch
{
    protected $config = [
        "127.0.0.1:9200",
    ];
    protected $es;

    /*
     * @Title:  构造函数
     * @Author: 北桥苏
     * @Times:  2020/5/14 17:35
     * */
    public function __construct()
    {
        $this->es = $this->init();
    }

    /*
     * @Title ElasticSearch服务初始化
     * @Return object ES实例
     * */
    private function init()
    {
        // 从配置文件读取 Elasticsearch 服务器列表
        $builder = ESClientBuilder::create()->setHosts($this->config);

        // 应用调试模式
//        if (env('APP_DEBUG')) {
//            // 配置日志，Elasticsearch 的请求和返回数据将打印到日志文件中，方便我们调试
//            $builder->setLogger(app('log')->error());
//        }
        return $builder->build();
    }

    public function test()
    {
        //return app('log')->error();
    }

    /*
     * @Title: 获取ES服务信息
     * */
    public function info()
    {
        return $this->es->info();
    }

    // 创建索引
    public function index($name, $mappings=[])
    {
//        $params = [
//            'index' => $name,
//            'body' => [
//                'settings' => [
//                    'number_of_shards' => 1,
//                    'number_of_replicas' => 0,
//                ],
//                'mappings' => [
//                    'properties' => $mappings
//                ]
//            ]
//        ];
        $params = [
            'index' => $name,
            'body' => [
                'settings' => [
                    'number_of_shards' => 1,
                    'number_of_replicas' => 0,
                ]
            ]
        ];
        if($mappings) {
            $params['body']['mappings']['properties'] = $mappings;
        }

        return $this->es->indices()->create($params);
    }

    // 获取索引配置,可以获取单个索引，不传参数是节点上的所有索引
    public function getIndexConfig($name=[])
    {
        $params = [];
        if(!empty($name)) {
            $params = [
                "index" => $name
            ];
        }
        return $this->es->indices()->getSettings($params);
    }

    // 创建/更改映射(创建索引时也可以指定映射-其实就是存储数据的字段类型)
    public function putMapping($name, $data)
    {
        $params = [
            'index' => $name,
            'body' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => $data
            ]
        ];

        return $this->es->indices()->putMapping($params);
    }

    // 获取映射
    public function getMapping($name)
    {
        $params = ['index' => $name];
        return $this->es->indices()->getMapping($params);
    }

    /**
     * @Notes: 获取索引中的文档
     * @param $name 索引名称
     * @param $id 编号
     * @author: ts
     * @Time: 2020/4/6 10:59
     */
    public function getIndex($name, $id)
    {
        return $this->es->get(['index' => $name, 'id' => $id]);
    }

//     public function getIndex($name)
//     {
//        return $this->es->get(['index' => $name]);
//     }

    /**
     * @Notes: 删除索引
     * @param $name
     * @return array
     * @author: ts
     * @Time: 2020/4/6 11:31
     */
    public function deleteIndex($name)
    {
        return $this->es->indices()->delete(['index' => $name]);
    }


    /***********************文档部分********************************************************/

    // 创建文档
    public function createDoc($name, $data)
    {
        $params = [
            'index' => $name,
            'id'    => $data['id'],
            'body'  => $data
        ];
        $response = $this->es->index($params);
        return $response;
    }

    /**
     * @Notes: 更新文档
     * @param $index 索引
     * @param $id 文档 id
     * @param $data 字段
     * @author: 北桥苏
     * @Time: 2020/4/21 9:51
     */
    public function updateDoc($index, $id, array $data)
    {
        $params = [
            'index' => $index,
            'id' => $id,
            'type' => '_doc',
            'body' => [
                'doc' => $data
            ]
        ];

        $this->es->update($params);
    }

    // 删除一个文档
    public function delDoc($param)
    {
        $this->es->delete($param);
    }

    // 批量创建文档
    public function bulk($params)
    {
        return $this->es->bulk($params);
    }

    // 搜索
    public function queryData($index,$kye, $from = 0, $size = 10)
    {
        $params = [
            'index' => $index,
            'body'  => [
                'from'  => $from,
                'size'  => $size,
                'query' => [
                    'match' => [
                        'class_name'  => '版',
                    ],
                ],
//                'sort' => $sort,
            ],
        ];

        return $this->es->search($params);
    }

    // 多功能搜索
    public function query($index,$where,$sort='',$from=0,$size=1500)
    {
        $params = [
            "index" => $index,
            "body" => [
                "from" => $from,
                "size" => $size,
                "query" => $where
            ]
        ];

        if($sort) {
            $params['body']['sort'] = $sort;
        }

        return $this->es->search($params);
    }



}